package com.earnings.okhttputils.utils.ui.activity.user;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.earnings.R;
import com.earnings.okhttputils.utils.OkHttp.OkHttpUtils;
import com.earnings.okhttputils.utils.UrlConstant.HttpUrl;
import com.earnings.okhttputils.utils.adapter.YBaseAdapter;
import com.earnings.okhttputils.utils.bean.RecordData;
import com.earnings.okhttputils.utils.bean.TreasureListData;
import com.earnings.okhttputils.utils.god.GodAdapter.GodBaseAdapter;
import com.earnings.okhttputils.utils.god.GodAdapter.GodViewHolder;
import com.earnings.okhttputils.utils.god.GodLeftHandBaseActivity;
import com.earnings.okhttputils.utils.god.Listener.GodOnClickListener;
import com.earnings.okhttputils.utils.https.HttpArrayCallback;
import com.earnings.okhttputils.utils.https.HttpMap;
import com.earnings.okhttputils.utils.ui.dialog.DetailDialog;
import com.earnings.okhttputils.utils.utils.SwipeRefresh;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import static com.earnings.R.id.mouth;


/**
 * 补贴明细
 * Created by Administrator on 2017/5/5 0005.
 */

public class UserTreasureListActivity extends GodLeftHandBaseActivity implements YBaseAdapter.setOnClickListener, GodOnClickListener {

    private RecyclerView recyclerview;
    private GodBaseAdapter<TreasureListData> adatper;
    private ArrayList<TreasureListData> treasureData;
    private DetailDialog detailDialog;
    private Calendar time = Calendar.getInstance();
    private String monthtime;
  //  private int year,mouth;

    @Override
    protected int onCreateLayoutId() {
        return R.layout.activity_recyclerview_refresh;
    }

    @Override
    protected void init() {
        setTitle("财币明细");
        treasureData=new ArrayList<>(  );
      //  year=time.get(Calendar.YEAR) ;
      //  mouth=time.get(Calendar.MONTH) + 1;
        adatper=new GodBaseAdapter<TreasureListData>(R.layout.item_recesovable_record,treasureData) {
            @Override
            public void onBind(GodViewHolder holder, int position, TreasureListData data) {
                if(position!=0){
                    if (!data.getCreatetime().substring(0,7  ).equals( treasureData.get( position - 1 ).getCreatetime().substring(0,7  ) ))
                        holder.setVisibility( mouth, View.VISIBLE );
                    else  holder.setVisibility( mouth, View.GONE );}
                else
                    holder.setVisibility( mouth, View.VISIBLE );

                holder.setText( mouth, data.getCreatetime().substring(0,7)   );
            //    holder.setText( R.id.name, data.getTitle() );
                if(data.getRich().indexOf("-")>-1){
                    holder.setText( R.id.price, data.getRich() );
                    holder.setTextColor(R.id.price,getResources().getColor(R.color.textred));}
                else    {
                    holder.setText( R.id.price, "+"+data.getRich() );
                    holder.setTextColor(R.id.price,getResources().getColor(R.color.style));
                }
                holder.setText( R.id.info, data.getContent() );
                holder.setText( R.id.date,  data.getCreatetime()  );
                Glide.with( getContext() ).load( data.getImg() ).into( (ImageView) holder.getView( R.id.icon ) );
            }
        };
        setRecyclerViewLayoutManager(R.id.recyclerview, new LinearLayoutManager(this)).setAdapter(adatper);

        new SwipeRefresh(this,R.id.refresh,R.id.recyclerview) {
            @Override
            public void pullDown() {
                showProgress();
              page=1;
                treasureData.clear();
                loadData();
            }

            @Override
            public void pullUp() {
                page++;

                loadData();
            }
        };
        showProgress();
        loadData();
    }



    int page=1;
    private void loadData() {
        Map map=new HttpMap( this );
      //  Calendar time = Calendar.getInstance();

        map.put( "p" ,page + "");
      //  map.put( "month" ,mouth+ "");
        OkHttpUtils.post().url( HttpUrl.BIZ_TREASURE_LIST_URL).params(map).build().execute( new HttpArrayCallback<RecordData>(this) {

            @Override
            public void onSuccess(List<RecordData> object, String msg) {

                for (int i = 0; i < object.size(); i++) {
                    List<TreasureListData> list = object.get( i ).getList();
                    treasureData.addAll( list );

                }
               // if(adatper==null)
                    adatper.notifyDataSetChanged();

               // treasureData.addAll( (ArrayList<TreasureListData>)object );
               // adatper.notifyDataSetChanged();
                dismissProgress();
            }

            @Override
            public void onFail(int code, String msg) {
                dismissProgress();

            }
        });

    }

    @Override
    public void ItemOnClickListener(int position, Object data) {
        //    skipActivity(BillDetailsActivity.class);

    }


    @Override
    public void OnClickListener(View view) {

    }
}
